<?php
namespace App\Http\Controllers\Api\V2\MyCenter;

use App\Http\Controllers\ApiController;
use App\Http\Controllers\Business\App\MyCenter\MyCenter;
use App\Http\Controllers\Business\App\Auth\AppAuth;
use App\Http\Controllers\Business\App\PublicBusiness;
use App\Http\Controllers\Business\App\Salesman\SalesmanManagement;

class MyCenterController extends ApiController
{
    //首页
    public function index() {
        $userId = $this->userId;
        $result = MyCenter::Index($userId);
        if(isset($result['status']) && ($result['status']== false)) {
            return $this->responseError($result['message']);
        }
        $data = $result['data'];
        $message = '';
        return $this->responseSuccess($message, $data);
    }
    //获取用户信息,钱包，个人中心等信息
    public function getUser() {
        $userId = $this->userId;
        $result = MyCenter::getUserInfoByUserId($userId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessData($result['data']);
        }
    }
    //个人中心中的个人信息
    public function getUserInfo() {
        $userId = $this->userId;
        $result = MyCenter::getUserInfo($userId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            $data = $result['data'];
            $message = '';
            return $this->responseSuccess($message, $data);
        }
    }
    //编辑个人信息
    public function editUserInfo() {
        $userId = $this->userId;
        //$avatar = $this->request->get('avatar');
        $nickName = $this->request->get('nickname');
        $sex = $this->request->get('sex');
        $birthday = $this->request->get('birthday');
        $city = $this->request->get('city');
        $result = MyCenter::saveUserInfo($userId,$nickName,$sex,$birthday,$city);
        if($result['status'] == 'success') {
            return $this->responseSuccessMessage($result['message']);
        } else {
            return $this->responseError($result['message']);
        }
    }
    //修改个人头像
    public function editUserAvatar() {
        $userId = $this->userId;
        $avatar = $this->request->get('avatar');
        $result = MyCenter::saveUserAvatar($userId,$avatar);
        if($result['status'] == 'success') {
            return $this->responseSuccessMessage($result['message']);
        } else {
            return $this->responseError($result['message']);
        }
    }
    public function recommend() {
        $userId = $this->userId;
        $result = MyCenter::getUserInfo($userId);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $userData = $result['data'];
        $userId =  PublicBusiness::encrypt($userId, 'E', ''); //加密
        $userId = rawurlencode($userId);
        //$userId = urlencode($userId); //URL编码
        $data = [
            'parentid' => $userId,
            'avatar' => $userData['avatar'],
            'nickname' => $userData['nickName'],
        ];
        $param = http_build_query($data);
        $url = env('WEB_URL') . "/mobile/mycenter/recommend?".$param;
        return redirect()->to($url);
    }
    //翻贝券列表
    public function doubleCouponList() {
        $userId = $this->userId;
        $type = $this->request->get('type'); //type 1表示可用，0表示不可用
        if(($type != '0') && ($type != '1')) {
            return $this->responseError('类型不对');
        }
        if($type == '1') {
            $result = MyCenter::doubleCouponsAvailableList($userId);
        } else {
            $result = MyCenter::doubleCouponsNotAvailableList($userId);
        }
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $message = '';
        $data = $result['data'];
        return $this->responseSuccess($message, $data);
    }
    //红包列表
    public function redEnvelopesList() {
        $userId = $this->userId;
        $type = $this->request->get('type');
        if(($type != '0') && ($type != '1')) {
            return $this->responseError('类型不对');
        }
        if($type == '1') {
            $result = MyCenter::redEnvelopesAvailableList($userId);
        } else {
            $result = MyCenter::redEnvelopesNotAvailableList($userId);
        }
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $message = '';
        $data = $result['data'];
        return $this->responseSuccess($message, $data);
    }
    //积分列表
    public function integralList() {
        $userId = $this->userId;
    }
    //收藏列表
    public function collectionList() {
        $userId = $this->userId;
        if($this->request->has('page')) {
            $page = $this->request->get('page');
        } else {
            $page = 1;
        }
        $length = 20;
        $result = MyCenter::collectionList($userId, $page, $length);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $message = '';
        $data = $result['data'];
        return $this->responseSuccess($message, $data);
    }
    //更新收藏状态
    public function updateCollection() {
        $userId = $this->userId;
        $type = $this->request->get('type');
        $shopId = $this->request->get('shopid');
        if($type != '1' && $type != '0') {
            return $this->responseError('类型错误');
        }
        if($type == '1') {
            $result = MyCenter::addCollection($userId, $shopId);
        } else {
            $result = MyCenter::delCollectionByShopId($userId, $shopId);
        }
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //添加收藏
    public function addCollection() {
        $userId = $this->userId;
        $shopId = $this->request->get('shopid');
        $result = MyCenter::addCollection($userId, $shopId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            $returnData = [];
            return $this->responseSuccess($result['message'], $returnData);
        }
    }
    //删除收藏
    public function deleteCollection() {
        $userId = $this->userId;
        $collectionId = $this->request->get('collectionid');
        $result = MyCenter::delCollection($userId, $collectionId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            $returnData = [];
            return $this->responseSuccess($result['message'], $returnData);
        }
    }
    //订阅表
    public function subscriptionList() {
        $userId = $this->userId;
        if($this->request->has('page')) {
            $page = $this->request->get('page');
        } else {
            $page = 1;
        }
        $length = 20;
        $result = MyCenter::subscriptionList($userId, $page, $length);
        if($result['status'] == 'failed') {
            $returnData = [];
            return $this->responseSuccess($result['message'], $returnData);
        }
        $data = $result['data'];
        $message = '';
        return $this->responseSuccess($message, $data);
    }
    //更新订阅状态
    public function updateSubscription() {
        $userId = $this->userId;
        $type = $this->request->get('type');
        $shopId = $this->request->get('shopid');
        if($type != '1' && $type != '0') {
            return $this->responseError('类型错误');
        }
        if($type == '1') {
            $result = MyCenter::addSubscription($userId, $shopId);
        } else {
            $result = MyCenter::delSubscriptionByShopid($userId, $shopId);
        }
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //添加订阅
    public function addSubscription() {
        $userId = $this->userId;
        $shopId = $this->request->get('shopid');
        $result = MyCenter::addSubscription($userId, $shopId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            $returnData = [];
            return $this->responseSuccess($result['message'], $returnData);
        }
    }
    //删除订阅
    public function deleteSubscription() {
        $userId = $this->userId;
        $subscriptionId = $this->request->get('subscriptionid');
        $result = MyCenter::delSubscription($userId, $subscriptionId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            $returnData = [];
            return $this->responseSuccess($result['message'], $returnData);
        }
    }
    //获取用户实名信息
    public function getIdentity() {
        $userId = $this->userId;
        $result = MyCenter::getIdentity($userId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessData($result['data']);
        }
    }
    //验证支付密码
    public function verifyPayPassword() {
        $userId = $this->userId;
        $payPayPassword = $this->request->get('paypassword');
        $result = MyCenter::validataPayPassword($userId, $payPayPassword);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //实名认证
    public function saveIdentity() {
        $userId = $this->userId;
        $realName = $this->request->get('realname');
        $identityCard = $this->request->get('identitycard');
        $result = MyCenter::validataIdentity($userId, $realName, $identityCard);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //检测认证信息是否正确
    public function verifyIdentity() {
        $userId = $this->userId;
        $realName = $this->request->get('realname');
        $identityCard = $this->request->get('identitycard');
        $result = MyCenter::verifyIdentity($userId, $realName, $identityCard);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //绑定手机
    public function updateMobile() {
        $userId = $this->userId;
        $oldMobile = $this->request->get('oldmobile');
        $newMobile = $this->request->get('newmobile');
        $code = $this->request->get('code');
        $result = MyCenter::updateMobile($userId, $oldMobile, $newMobile, $code);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //设置支付密码
    public function setPayPassword() {
        $userId = $this->userId;
        $result = MyCenter::setPayPassword($userId, $this->request->get('paypassword'));
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            $resultData = [];
            return $this->responseSuccess($result['message'], $resultData);
        }
    }
    //删除支付密码
    public function deletePayPassword() {
        $userId = $this->userId;
        $result = MyCenter::delPayPassword($userId, $this->request->get('paypassword'));
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            $resultData = [];
            return $this->responseSuccess($result['message'], $resultData);
        }
    }
    //重置密码
    public function resetPayPassword() {
        $userId = $this->userId;
        if(!$this->request->has('type')) {
            return $this->responseError('请选择类型');
        }
        $type = $this->request->get('type'); //'1'表示通过旧密码来修改密码， '2'表示通过身份认证修改密码
        if($type == '1') {
            $oldPayPassword = $this->request->get('oldpaypassword');
            $newPayPassword = $this->request->get('newpaypassword');
            $result = MyCenter::updatePayPasswordForOldPayPassword($userId, $oldPayPassword, $newPayPassword);
            if($result['status'] == 'failed') {
                return $this->responseError($result['message']);
            } else {
                $returnData = [];
                return $this->responseSuccess($result['message'], $returnData);
            }
        } elseif($type == '2') {
            $realName = $this->request->get('realname');
            $identityCard = $this->request->get('identitycard');
            $newPayPassword = $this->request->get('newpaypassword');
            $result = MyCenter::updatePayPasswordForValidataIdentity($userId, $realName, $identityCard, $newPayPassword);
            if($result['status'] == 'failed') {
                return $this->responseError($result['message']);
            } else {
                $returnData = [];
                return $this->responseSuccess($result['message'], $returnData);
            }
        } else {
            return $this->responseError('无效类型');
        }
    }
    //商家入住
    public function shopJoinIn() {
        $url = env('WEB_URL') . '/mobile/mycenter/shop/joinin';
        return redirect()->to($url);
    }
    //用户的个人地推首页
    public function mySalesmanIndex() {
        $userId = $this->userId;
        if($this->request->has('cityid')) {
            $cityId = $this->request->get('cityid');
        } else {
            $cityId = 1;
        }
        $qrCodeUrl = env('WEB_URL') . "qrcode/user/" . $userId ; //个人二维码
        $result = SalesmanManagement::userSalesmanIndex($userId, $cityId);
        if($result['status'] == 'failed') {
            $isShowSalesmanOrder = 0; //是否显示地推订单
            $isShowSalesmanRecharge = 0; //是否显示地推充值
            $salesmanRules = ''; //地推规则
        } else {
            $isShowSalesmanOrder = $result['data']['isshowsalesmanorder'];
            $isShowSalesmanRecharge = $result['data']['isshowsalesmanrecharge'];
            $salesmanRules = $result['data']['salesmanrules'];
        }
        $isShowSalesmanWithdrawal = 0;
        $data = [
            'qrcodeurl' => $qrCodeUrl,
            'isshowsalesmanorder' => $isShowSalesmanOrder,
            'isshowsalesmanrecharge' => $isShowSalesmanRecharge,
            'isshowsalesmanwithdrawal' => $isShowSalesmanWithdrawal,
            'salesmanrules' => $salesmanRules,
        ];
        return $this->responseSuccessData($data);
    }

    //地推注册用户
    public function salesmanRegisterList() {
        $userId = $this->userId;
        $startTime = $this->request->get('starttime');
        $lastTime = $this->request->get('lasttime');
        if($this->request->has('page')) {
            $page = $this->request->get('page');
        } else {
            $page = 1;
        }
        $length = 20;
        $result = SalesmanManagement::userSalesmanRegisterList($userId, $startTime, $lastTime, $page, $length);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessData($result['data']);
        }
    }
    //地推订单
    public function salesmanOrderList() {
        $userId = $this->userId;
        $startTime = $this->request->get('starttime');
        $lastTime = $this->request->get('lasttime');
        if($this->request->has('page')) {
            $page = $this->request->get('page');
        } else {
            $page = 1;
        }
        $length = 20;
        $result = SalesmanManagement::userSalesmanOrderList($userId, $startTime, $lastTime, $page, $length);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessData($result['data']);
        }
    }
    //地推充值
    public function salesmanRechargeList() {
        $userId = $this->userId;
        $startTime = $this->request->get('starttime');
        $lastTime = $this->request->get('lasttime');
        if($this->request->has('page')) {
            $page = $this->request->get('page');
        } else {
            $page = 1;
        }
        $length = 20;
        $result = SalesmanManagement::userSalesmanRechargeList($userId, $startTime, $lastTime, $page, $length);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessData($result['data']);
        }
    }
    //地推结款
    public function salesmanWithdrawal() {
        $userId = $this->userId;
        if($this->request->has('cityid')) {
            $cityId = $this->request->get('cityid');
        } else {
            $cityId = 1;
        }
        $result = SalesmanManagement::userSalesmanWithdrawal($userId, $cityId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessData($result['data']);
        }
    }
    //通过日期获取结款详情
    public function salesmanWithdrawalDetailByDay() {
        $userId = $this->userId;
        $myDate = $this->request->get('accounttime');
        $versionCode = $this->request->get('versioncode');
        if($versionCode >= 15) {
            $result = SalesmanManagement::userSalesmanWithdrawalListByDayAndV15Version($userId, $myDate);
        } else {
            $result = SalesmanManagement::userSalesmanWithdrawalListByDay($userId, $myDate);
        }
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessData($result['data']);
        }
    }
    //地推余额
    public function salesmanBalance() {
        $userId = $this->userId;
        $versionCode = $this->request->get('versioncode');
        if($versionCode >= 15) {
            $result = SalesmanManagement::userSalesmanBalanceByV15Version($userId);
        } else {
            $result = SalesmanManagement::userSalesmanBalance($userId);
        }
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessData($result['data']);
        }
    }
}